package cn.peach.pet.service.impl;

import cn.peach.basic.service.impl.BaseServiceImpl;
import cn.peach.basic.util.DistanceUtil;
import cn.peach.basic.util.PageList;
import cn.peach.basic.util.Point;
import cn.peach.org.domain.Employee;
import cn.peach.org.domain.Shop;
import cn.peach.org.mapper.EmployeeMapper;
import cn.peach.org.mapper.ShopMapper;
import cn.peach.pet.domain.Pet;
import cn.peach.pet.domain.SearchMasterMsg;
import cn.peach.pet.mapper.SearchMasterMsgMapper;
import cn.peach.pet.query.SearchMasterMsgQuery;
import cn.peach.pet.service.IPetService;
import cn.peach.pet.service.ISearchMasterMsgService;
import cn.peach.user.domain.User;
import cn.peach.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IPetService petService;
    /**
     * 寻主发布
     * @param smm
     */
    @Override
    public void publish(SearchMasterMsg smm, Long logininfoId) {
        // 校验： 略
        // 获取登录用户id
        User user = userMapper.findByLogininfoId(logininfoId);
        smm.setUser_id(user.getId());
        smm.setUser(user);
        // 获取当前用户输入的位置
        Point point = DistanceUtil.getPoint(smm.getAddress());
        // 查询所有店铺
        List<Shop> shops = shopMapper.loadAll();
        // 获取最近的店铺
        Shop shop = DistanceUtil.getNearestShop(point, shops);
        smm.setShop(shop);
        smm.setShop_id(shop.getId());
        searchMasterMsgMapper.add(smm);
    }

    /**
     * 待处理
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long logininfoId) {
        // 根据logininfoid查询员工employee
        Employee employee = employeeMapper.findByLogininfoId(logininfoId);
        // 看员工获取的店铺id是否为空
        if (employee.getShop_id() != null) {
            query.setShopId(employee.getShop_id());
        }
        query.setState(0);
        PageList<SearchMasterMsg> pageList = super.queryList(query);
        return pageList;
    }

    /**
     * 已处理
     * @param query
     * @param id
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long logininfoId) {
        // 根据logininfo查询到员工employee
        Employee employee = employeeMapper.findByLogininfoId(logininfoId);
        // 判断获取的店铺id是否为空
        if (employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        // 设置状态为1 已处理
        query.setState(1);
        PageList<SearchMasterMsg> pageList = super.queryList(query);
        return pageList;
    }

    /**
     * 保存数据
     * @param pet
     * @param id
     */
    @Override
    public void handle(Pet pet, Long id) {
        // 改SMM状态
        searchMasterMsgMapper.updateStateById(pet.getSearch_master_msg_id());
        // 向pet和PetDetail中存数据
        petService.insert(pet);
    }
}
